﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEViolationCategoryHSEService
{
    private readonly SimpleDbContext _context;
    public QHSEViolationCategoryHSEService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEViolationCategoryHSEModel>> GetAsync()
    {
        var violationHSEs = await _context.Set<QHSESysViolationCategoryHSE>().ToListAsync();
        return MapperHelper.Map<List<QHSEViolationCategoryHSEModel>>(violationHSEs);
    }
    public async Task<QHSEViolationCategoryHSEPageResultModel> GetResult(QHSEViolationCategoryHSEPageInputModel inputModel)
    {
        var violationCategory = await _context.Set<QHSESysViolationCategory>().FindAsync(inputModel.Id);
        var hses = new List<QHSESysViolationHSE>();
        if (violationCategory.Item == "其它" || violationCategory.Item == "其他")
        {
            hses = await _context.Set<QHSESysViolationHSE>().AsNoTracking().ToListAsync();
        }
        else
        {
            hses = await _context.Set<QHSESysViolationCategoryHSE>()
                .AsNoTracking()
             .Join(_context.Set<QHSESysViolationHSE>(), vcHse => vcHse.HSECode, Hse => Hse.Code, (vcHse, Hse) => new { vcHse, Hse })
             .Where(u => u.vcHse.Code == violationCategory.Code)
             .Select(x => x.Hse).ToListAsync();
        }
        var hseModels = MapperHelper.Map<List<QHSEViolationHSEModel>>(hses);
        var resultModel = new QHSEViolationCategoryHSEPageResultModel
        {
            Result = hseModels
        };
        return resultModel;
    }
}
